package myJs.api

import japgolly.scalajs.react.extra.Ajax
import myJs.myPkg.MyFetch
import org.scalajs.macrotaskexecutor.MacrotaskExecutor.Implicits._
import shared.pojo.Pojo.{ValidData, data}
import upickle.default._
import myJs.Implicits._
import myJs.tool.Tool
import shared.pojo.mission.{CorHeatmapPlot, PlotConfigDataRequest}

/** Created by yz on 18/10/2022
  */
object MissionTool {

  import shared.pojo.Pojo.missionTool._

  val baseUrl = s"${Api.apiUrl}/user/missionTool"

  def workspaceImage(id: String, fileName: String) =
    s"${baseUrl}/workspaceImage?id=${id}&fileName=${fileName.encodeURI}"

  def checkFileExist(request: CheckFileExist.Request) = {
    val url = s"${baseUrl}/checkFileExist"
    Ajax
      .post(s"${url}")
      .setRequestContentTypeJson
      .send(write(request))
      .asAsyncCallback
      .map { xhr =>
        read[Boolean](xhr.responseText)
      }
  }

  def getHeaders(request: GetHeaders.Request) = {
    val url = s"${baseUrl}/getHeaders"
    Ajax
      .post(s"${url}")
      .setRequestContentTypeJson
      .send(write(request))
      .asAsyncCallback
      .map { xhr =>
        read[List[String]](xhr.responseText)
      }
  }

  def isPaired(request: IsPaired.Request) = {
    val url = s"${baseUrl}/isPaired"
    Ajax
      .post(s"${url}")
      .setRequestContentTypeJson
      .send(write(request))
      .asAsyncCallback
      .map { xhr =>
        read[Boolean](xhr.responseText)
      }
  }

  def myPlot(request: String) = {
    val url = s"${baseUrl}/myPlot"
    Ajax
      .post(s"${url}")
      .setRequestContentTypeJson
      .send(request)
      .asAsyncCallback
      .map { xhr =>
        read[ValidData](xhr.responseText)
      }
  }

  def myPlotIO(request: String) = {
    val url = s"${baseUrl}/myPlot"
    Ajax
      .post(s"${url}")
      .setRequestContentTypeJson
      .send(request)
      .asAsyncCallback
      .map { xhr =>
        read[ValidData](xhr.responseText)
      }
  }

  def plotConfigData(request: PlotConfigDataRequest) = {
    val url = s"${baseUrl}/plotConfigData"
    Ajax
      .post(s"${url}")
      .setRequestContentTypeJson
      .send(write(request))
      .asAsyncCallback
      .map { xhr =>
        xhr.responseText
      }
  }

  def plotConfigDataIO(request: PlotConfigDataRequest) = {
    val url = s"${baseUrl}/plotConfigData"
    Ajax
      .post(s"${url}")
      .setRequestContentTypeJson
      .send(write(request))
      .asAsyncCallback
      .map { xhr =>
        (xhr.responseText)
      }
  }

  def downloadWorkspaceFile(id: String, fileName: String) =
    s"${baseUrl}/downloadWorkspaceFile?id=${id}&fileName=${fileName.encodeURI}"

}
